package come.example.pagetransformer

import android.content.Intent
import android.os.Bundle
import android.support.design.bottomnavigation.LabelVisibilityMode
import android.support.design.widget.BottomNavigationView
import android.support.v4.app.FragmentActivity
import android.support.v4.view.ViewPager
import android.util.Log
import android.view.View
import come.example.pagetransformer.adapter.TtFragmentPagerAdapter
import come.example.pagetransformer.fragment.TtFragment
import come.example.pagetransformer.transform.*
import come.example.pagetransformer.widget.RandomTextView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : FragmentActivity() {

    private val onNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.navigation_home -> {
                viewPaper.currentItem = 0
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_dashboard -> {
                viewPaper.currentItem = 1
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_notifications -> {
                viewPaper.currentItem = 2
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_mine -> {
                viewPaper.currentItem = 0
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initRandTextView()
        initView()
//        tvText.setOnClickListener { View.OnClickListener {
//            Log.e("initRandTextView","initRandTextView run")
//            startActivity(Intent(this@MainActivity, SecondActivity::class.java))
//        } }
        tvText.setOnClickListener {
            Log.e("initRandTextView", "initRandTextView run")
            startActivity(Intent(this@MainActivity, SecondActivity::class.java))
        }
    }

    private fun initRandTextView() {
        rtv.text = "23424"
        rtv.setMaxScrollLines(20)
        rtv.setScrollType(RandomTextView.SCROLL_TYPE_HIGH_FAST)
    }

    private fun initView() {
        val fragmentList = mutableListOf<TtFragment>()
        val bundle = Bundle()
        bundle.putString("text", "0")
        fragmentList.add(TtFragment.getInstance(bundle))

        val bundle1 = Bundle()
        bundle1.putString("text", "1")
        fragmentList.add(TtFragment.getInstance(bundle1))

        val bundle2 = Bundle()
        bundle2.putString("text", "2")
        fragmentList.add(TtFragment.getInstance(bundle2))

        val bundle3 = Bundle()
        bundle2.putString("text", "3")
        fragmentList.add(TtFragment.getInstance(bundle3))

//        val bundle4 = Bundle()
//        bundle4.putString("text","4")
//        fragmentList.add(TtFragment.getInstance(bundle4))

        val adapter = TtFragmentPagerAdapter(supportFragmentManager, fragmentList)
        viewPaper.adapter = adapter
        viewPaper.offscreenPageLimit = 3
        viewPaper.setCurrentItem((fragmentList.size) / 2, false)
        //viewPaper.setPageTransformer(true, AdTransformer())
        //viewPaper.setPageTransformer(true, CardTransformer())
        //viewPaper.setPageTransformer(true, StackTransformer())
        //viewPaper.setPageTransformer(true, AlphaTransformer(0.8f))
        //viewPaper.setPageTransformer(true, DepthPageTransformer())
        //viewPaper.setPageTransformer(true, EnhanceDepthPageTransformer())
        //viewPaper.setPageTransformer(true, FlipPageTransformer())
        //viewPaper.setPageTransformer(true, RotateDownTransformer())
        viewPaper.setPageTransformer(true, RotateUpTransformer())
        navView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)
        navView.labelVisibilityMode = LabelVisibilityMode.LABEL_VISIBILITY_LABELED
        navView.itemTextAppearanceActive = R.style.Bnv_TextAppearanceActive
        navView.itemTextAppearanceInactive = R.style.Bnv_TextAppearanceInactive
        navView.itemBackgroundResource = R.color.colorBnvBackground

        navView.itemIconTintList = null

        //BottomNavigationViewHolder.disableCalculateTextScale(navView)
        viewPaper.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(p0: Int) {

            }

            override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {

            }

            override fun onPageSelected(p0: Int) {
                navView.menu.getItem(p0).isChecked = true
                rtv.start()
            }
        })
    }
}
